package com.cksys.achievement.exception;

import com.cksys.achievement.utils.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName: HandlerException
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2019/9/28 10:12
 */
@Slf4j
@RestControllerAdvice
public class HandlerException {

    @ExceptionHandler(value = {org.springframework.validation.BindException.class})
    public RestResponse bindException(BindException e) {
        e.printStackTrace();
        if (null != e.getFieldError()) {
            String defaultMessage = e.getFieldError().getDefaultMessage();
            log.error(defaultMessage);
            return RestResponse.fail(defaultMessage);
        } else {
            return RestResponse.fail("参数出现错误！");
        }
    }

    @ExceptionHandler(value = {Exception.class})
    public RestResponse exception(Exception e) {
        e.printStackTrace();
        log.info(e.getMessage());
        return RestResponse.fail(e.getMessage());
    }

}
